import { createRouter, createWebHistory } from 'vue-router'
import { usePortStore } from '@/stores/index'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      redirect: '/home',
      path: '/'
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('@/views/home/index.vue'),
      children: [
        {
          redirect: '/knowledge',
          path: '/home'
        },
        {
          path: '/aiDevice',
          name: 'aiDevice',
          component: () => import('@/views/aiDevice/index.vue'),
          // children
        },
        {
          path: '/pocketAi',
          name: 'pocketAi',
          component: () => import('@/views/pocketAi/index.vue'),
          children: [
            {
              redirect: '/knowledge',
              path: '/pocketAi'
            },
            {
              path: '/knowledge',
              name: 'knowledge',
              component: () => import('@/views/knowledge/index.vue'),
            },
            {
              path: '/search',
              name: 'search',
              component: () => import('@/views/search/index.vue'),
            },

            {
              path: '/ppt',
              name: 'ppt',
              component: () => import('@/views/ppt/index.vue'),
            },
            {
              path: '/Blog',
              name: 'Blog',
              component: () => import('@/views/Blog/index.vue'),
            }
          ]
        },
        {
          path: '/album',
          name: 'album',
          component: () => import('@/views/album/index.vue'),

        },
        {
          path: '/nas',
          name: 'nas',
          component: () => import('@/views/nas/index.vue'),

        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/index.vue'),

    },
    {
      path: '/generationBlog',
      name: 'generationBlog',
      component: () => import('@/views/generation/Blog.vue'),

    },
    {
      path: '/generationPPT',
      name: 'generationPPT',
      component: () => import('@/views/generation/PPT.vue'),

    },
    {
      path: '/setting',
      name: 'setting',
      component: () => import('@/views/setting/index.vue'),

    },
    {
      path: '/openSearch',
      name: 'openSearch',
      component: () => import('@/views/generation/openSearch.vue'),

    },
  ]
})



router.beforeEach((to, from, next) => {
  const { setPort, getPort, isInvalid } = usePortStore()
  if (to.path !== '/login' && isInvalid()) {
    console.log(from, "rom")
    setPort({})
    next(`/login`)
  }
  if (to.path === '/login' && !isInvalid()) {
    next('/knowledge')
  }
  if (to.path === '/nas') {
    // window.location.href = 'http://ts.aipocket-nas.igrsservice.com/#/'
  }
  next()
})

export default router
